﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MulagoDb.database;
using System.Collections;

namespace MulagoDb
{
    public partial class FileClosure : UserControl
    {
        public FileClosure()
        {
            InitializeComponent();
        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            deathMonthCombo.Enabled = true;
            deathYearsCombo.Enabled = false;
        }

        private void monthRadio_CheckedChanged(object sender, EventArgs e)
        {
            yearsCombo.Enabled = false;
            monthCombo.Enabled = true;
        }

        private void yearsRadio_CheckedChanged(object sender, EventArgs e)
        {
            monthCombo.Enabled = false;
            yearsCombo.Enabled = true;
        }

        private void domantMonthRadio_CheckedChanged(object sender, EventArgs e)
        {
            domantMonthCmbo.Enabled = true;
            domantYearsCombo.Enabled = false;
        }

        private void domantYearsRadio_CheckedChanged(object sender, EventArgs e)
        {
            domantYearsCombo.Enabled = true;
            domantMonthCmbo.Enabled = false;
        }

        private void deathYearsRadio_CheckedChanged(object sender, EventArgs e)
        {
            deathYearsCombo.Enabled = true;
            deathMonthCombo.Enabled = false;
        }

        private void FileClosure_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(deathRadio.Checked){

                Fclosure.insertfclosure_major(FindPatient.patientId, deathdatepicker.Text,
                    monthRadio.Checked ? deathMonthCombo.Text : deathYearsCombo.Text, locatioinCombo.Text, deathRadio.Text);
                //add death related items
                Fclosure.insertfclosure_death(FindPatient.patientId, acutechestcheck.Checked ? "T" : "F", 
                    probablemalariacheck.Checked ? "T" : "F", acutepaincheck.Checked ? "T" : "F",
                    multorganfailurecheck.Checked ? "T" : "F", getState(sepsisprobableTreeview).ToString(),
                    getState(strokeTreeview).ToString(), getState(mengitisprobableTreeview).ToString(), 
                    pulmonaryhypertensioncheck.Checked ? "T" : "F", chronicrenalfailurecheck.Checked ? "T" : "F", 
                    suddendeathcheck.Checked ? "T" : "F", othernonscdrelatedCheck.Checked ? 
                    othernonscdrelatedTfld.Text : "", tbsuspectedCheck.Checked ? "T" : "F", 
                    acutesplenicCheck.Checked ? "T" : "F", getState(severeanaemiaTreeview).ToString(), 
                    othernonscdrelatedCheck.Checked ? othernonscdrelatedTfld.Text : "F", unknowncheck.Checked ? "T" : "F");
                //
            }
            else if (patientmovedRadio.Checked) {
                Fclosure.insertfclosure_major(FindPatient.patientId, dateTimePickermoved.Text,
                    monthRadio.Checked ? monthCombo.Text : yearsCombo.Text, movedLocationTfld.Text,
                    patientmovedRadio.Text);
            }
            else if (dormantfileRadio.Checked) {
                Fclosure.insertfclosure_dormantfile(FindPatient.patientId, dateTimePickerdormant.Text,
                    domantMonthRadio.Checked ? domantMonthCmbo.Text : domantYearsCombo.Text);
            }
            else if (otherRadio.Checked) {
                Fclosure.insertfclosure_other(FindPatient.patientId, otherTfld.Text);
            }
            MessageBox.Show(this, "Successfully submitted");
        }
        public StringBuilder getState(TreeView treeview)
        {
            StringBuilder nodestates = new StringBuilder();

            foreach (TreeNode pnode in treeview.Nodes)
            {
                foreach (TreeNode tnode in pnode.Nodes)
                {
                    nodestates.Append(tnode.Checked ? "T," : "F,");
                }
            }
            return nodestates;
        }
    }
}
